//
//  LBCategorySectionView.m
//  jslj
//
//  Created by luckyRoy on 2017/10/10.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCategorySectionView.h"
#import "UIColor+LBColor.h"
#import "LBCategoryMainView.h"
#import <Masonry.h>
#import "UIImageView+WebCache.h"
#import "NSString+LBString.h"

static CGFloat const LBCategorySectionViewEdge = 20;

static CGFloat const LBCategorySectionViewTopLabelHeight = 40;

static CGFloat const LBCategorySectionViewCategoryLabelHeight = 20;

@interface LBCategorySectionView ()

@property (nonatomic, strong) UIButton *topHeaderButton;

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) UIButton *categoryButton;

@property (nonatomic, strong) UIButton *editorButton;

@property (nonatomic, copy) NSMutableAttributedString *topHeaderTitle;

@property (nonatomic, copy) NSMutableAttributedString *categoryTitle;

@end

@implementation LBCategorySectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createAllView];
    }
    return self;
}

- (void)updateUIByTopTitle:(NSMutableAttributedString *)topHeaderTitle
                OtherTitle:(NSMutableAttributedString *)categoryTitle
               categoryUrl:(NSString *)url
{
    self.topHeaderTitle = topHeaderTitle;
    self.categoryTitle = categoryTitle;
    
    [self.topHeaderButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(LBCategorySectionViewTopLabelHeight);
    }];
    
    if ([[self.topHeaderTitle string] isEqual:@"我的订阅"]) {
        self.topHeaderButton.backgroundColor = [UIColor whiteColor];
    } else {
        self.topHeaderButton.backgroundColor = [UIColor lightGray];
    }
    
    if (self.topHeaderTitle) {
        [self.topHeaderButton setAttributedTitle:self.topHeaderTitle forState:UIControlStateNormal];
        [self.topHeaderButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(LBCategorySectionViewTopLabelHeight);
        }];
        
    } else {
        [self.topHeaderButton setAttributedTitle:nil forState:UIControlStateNormal];
        [self.topHeaderButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
    if (self.categoryTitle) {
        [self.categoryButton setAttributedTitle:self.categoryTitle forState:UIControlStateNormal];
        [self.categoryButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(LBCategorySectionViewCategoryLabelHeight);
        }];
        NSURL *theUrl = [NSURL URLWithString:[url encodeURL]];
        [self.iconImageView sd_setImageWithURL:theUrl placeholderImage:[UIImage imageNamed:@"Category.bundle/lb_category_defaul_icon"]];
    } else {
        [self.categoryButton setAttributedTitle:nil forState:UIControlStateNormal];
        [self.iconImageView setImage:nil];
        [self.categoryButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
}

- (void)createAllView
{
    [self addSubview:self.topHeaderButton];
    [self addSubview:self.iconImageView];
    [self addSubview:self.categoryButton];
    [self addSubview:self.editorButton];
    
    [self.topHeaderButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.right.equalTo(self);
        make.left.offset(0);
        make.height.mas_equalTo(LBCategorySectionViewTopLabelHeight);
    }];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topHeaderButton.mas_bottom);
        make.left.offset(LBCategorySectionViewEdge);
        make.size.mas_equalTo(CGSizeMake(LBCategorySectionViewCategoryLabelHeight, LBCategorySectionViewCategoryLabelHeight));
    }];
    [self.categoryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(5);
        make.centerY.equalTo(self.iconImageView);
    }];
    [self.editorButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-LBCategorySectionViewEdge);
        make.centerY.equalTo(self.topHeaderButton);
        make.size.mas_equalTo(CGSizeMake(52, 24));
    }];
    
    [self.editorButton setHidden:YES];
}

#pragma mark - click

- (void)editorButtonClick
{
    NSString *buttonTitle = self.editorButton.titleLabel.text;
    
    if ([buttonTitle isEqualToString:@"完成"]) {
        [self.editorButton setTitle:@"编辑" forState:UIControlStateNormal];
        if (self.viewBlock) {
            self.viewBlock(NO);
        }
    } else {
        [self.editorButton setTitle:@"完成" forState:UIControlStateNormal];
        if (self.viewBlock) {
            self.viewBlock(YES);
        }
    }
}

#pragma mark - setter

- (void)setIsEditor:(BOOL)isEditor
{
    if (isEditor) {
        [self.editorButton setTitle:@"完成" forState:UIControlStateNormal];
    } else {
        [self.editorButton setTitle:@"编辑" forState:UIControlStateNormal];
    }
}

#pragma mark - getter

- (UIButton *)topHeaderButton
{
    if (!_topHeaderButton) {
        _topHeaderButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _topHeaderButton.titleLabel.font = [UIFont systemFontOfSize:16];
        UIColor *buttonColor = [UIColor colorWithRed:68/255.0 green:68/255.0 blue:68/255.0 alpha:1];
        [_topHeaderButton setTitleColor:buttonColor forState:UIControlStateNormal];
        [_topHeaderButton setContentEdgeInsets:UIEdgeInsetsMake(0, LBCategorySectionViewEdge, 0, 0)];
        _topHeaderButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
    return _topHeaderButton;
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc]init];
    }
    return _iconImageView;
}

- (UIButton *)categoryButton
{
    if (!_categoryButton) {
        _categoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _categoryButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_categoryButton setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1] forState:UIControlStateNormal];
        [_categoryButton setTitleEdgeInsets:UIEdgeInsetsMake(0,5, 0,-5)];
        [_categoryButton setImageEdgeInsets:UIEdgeInsetsMake(0,5, 0,5)];
    }
    return _categoryButton;;
}

- (UIButton *)editorButton
{
    if (!_editorButton) {
        _editorButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _editorButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_editorButton setTitle:@"编辑" forState:UIControlStateNormal];
        [_editorButton setTitleColor:[UIColor blue] forState:UIControlStateNormal];
        [_editorButton setTitleColor:[UIColor darkBlue] forState:UIControlStateHighlighted];
        _editorButton.layer.cornerRadius = 12;
        _editorButton.layer.borderColor = [UIColor blue].CGColor;
        _editorButton.layer.borderWidth = 1/[UIScreen mainScreen].scale;
        [_editorButton addTarget:self action:@selector(editorButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _editorButton;
}

+ (CGFloat)heightForViewTopState:(BOOL)isTopTitle
                   categoryState:(BOOL)isCategoryTitle
{
    CGFloat totalHeight = 0;
    if (isTopTitle) {
        totalHeight += LBCategorySectionViewTopLabelHeight;
    }
    if (isCategoryTitle) {
        totalHeight += LBCategorySectionViewCategoryLabelHeight;
    }
    return totalHeight;
}

@end
